import router from '@/config/router'

export const availableContentWidth = (width) =>{
  return `${document.documentElement.clientWidth}` - width;//减去宽度
}
export const availableContentHeight = (height) =>{
  return `${document.documentElement.clientHeight}` - height;//减去高度
}

export const routerSkip = (path,query) => {
  if(query === undefined){
    router.push(path)
  }else {
    router.push({path:path,query:query})
  }
};

//分页
export const pagination = (changePage, showSizeChange)=>{
  return{
    current:1,//默认初始页
    defaultPageSize:10,//默认每页数量
    showQuickJumper:true,//分页跳转
    showSizeChanger:true,//显示每页数量
    pageSizeOptions:['5','10','20','50','100'],//每页数量选项
    total:0,
    showTotal:total => `共${total}条数据`,
    onChange: (current,pageSize) => changePage(current,pageSize),
    onShowSizeChange:(current,pageSize) => showSizeChange(current,pageSize),
  }
}


//图像
export const pic2Base64 = (file) => {
  return new Promise(function (resolve, reject) {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = e => {
      resolve(e.target.result);
    };
  });
};
//查找方法
export const selectTree = (value, gData, expandedKeys) => {
  const that = this;
  return gData.map((item) => {
    return getParentValue(value, item, expandedKeys);
  }).filter((item, i, self) => item && self.indexOf(item) === i);
};
// 查找树的节点
const getParentValue = (value, tree, expandedKeys) => {
  let parentValue;
  if (tree.title.indexOf(value) > -1) {
    parentValue = {
      key: tree.key,
      title: tree.title,
    };
    if (tree.children !== undefined) {
      parentValue.children = [];
      for (let t of tree.children) {
        let childNode = getParentValue(value, t, expandedKeys);
        if (childNode !== undefined) {
          parentValue.children.push(childNode);
        }
      }
    }
    if (expandedKeys !== undefined) {
      expandedKeys.push(parentValue.key);
    }
    return parentValue
  } else {
    if (tree.children !== undefined) {
      parentValue = {
        key: tree.key,
        title: tree.title,
        children: []
      };
      for (let t of tree.children) {
        let childNode = getParentValue(value, t, expandedKeys);
        if (childNode !== undefined) {
          parentValue.children.push(childNode);
        }
      }
      if (parentValue.children.length !== 0) {
        return parentValue;
      }
    }
  }
};
